package com.chinapost.service.impl;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinapost.dao.BaseDaoI;
import com.chinapost.excelUtil.ExcelUtil;
import com.chinapost.po.Tdept;
import com.chinapost.po.Twdxx;
import com.chinapost.service.WdxxServiceI;
import com.chinapost.util.DoubleUtil;
import com.chinapost.vo.DataGrid;
import com.chinapost.vo.Wdxx;

@Service("wdxxService")
public class WdxxServiceImpl implements WdxxServiceI {

	private BaseDaoI<Twdxx> wdxxDao;
	private BaseDaoI<Tdept> deptDao;

	public BaseDaoI<Twdxx> getWdxxDao() {
		return wdxxDao;
	}

	@Autowired
	public void setWdxxDao(BaseDaoI<Twdxx> wdxxDao) {
		this.wdxxDao = wdxxDao;
	}

	public BaseDaoI<Tdept> getDeptDao() {
		return deptDao;
	}

	@Autowired
	public void setDeptDao(BaseDaoI<Tdept> deptDao) {
		this.deptDao = deptDao;
	}

	@Override
	public DataGrid datagrid(Wdxx wdxx) {
		DataGrid dg = new DataGrid();
		dg.setRows(listRows(find(wdxx)));
		dg.setTotal(total(wdxx));
		return dg;
	}

	private Long total(Wdxx wdxx) {
		String hql = "select count(*) from Twdxx t where 1=1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(wdxx, hql, params);
		return wdxxDao.count(hql, params);
	}

	private List<Twdxx> find(Wdxx wdxx) {
		String hql = "from Twdxx t where 1=1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(wdxx, hql, params);
		if (wdxx.getOrder() != null && wdxx.getSort() != null) {
			hql += " order by " + wdxx.getSort() + " " + wdxx.getOrder();
		}
		return wdxxDao.find(hql, params, wdxx.getPage(), wdxx.getRows());
	}

	private List<Wdxx> listRows(List<Twdxx> lt) {
		List<Wdxx> rows = new ArrayList<Wdxx>();
		if (lt != null && lt.size() > 0) {
			for (Twdxx t : lt) {
				Wdxx sub = new Wdxx();
				BeanUtils.copyProperties(t, sub);
				sub.setDeptId(t.getTdept().getCid());
				sub.setDeptName(t.getTdept().getCname());
				rows.add(sub);
			}
		}
		return rows;
	}

	private String addWhere(Wdxx wdxx, String hql, Map<String, Object> params) {
		if (wdxx != null && wdxx.getCid() != null && !wdxx.getCid().trim().equals("")) {
			hql += " and t.cid = :id ";
			params.put("id", wdxx.getCid());
		}
		if (wdxx != null && wdxx.getCname() != null && !wdxx.getCname().trim().equals("")) {
			hql += " and t.cname like :wdxxname ";
			params.put("wdxxname", "%%" + wdxx.getCname() + "%%");
		}
		if (wdxx != null && wdxx.getDeptIds() != null && wdxx.getDeptIds().size() > 0) {
			hql += " and t.tdept.cid in(:deptids)";
			params.put("deptids", wdxx.getDeptIds());
		}
		return hql;
	}

	@Override
	public List<Wdxx> listWdxx(String deptid) {
		Tdept tdept = deptDao.get(Tdept.class, deptid);
		List<Twdxx> wds = new ArrayList<Twdxx>();
		if (tdept.getTdepts() == null || tdept.getTdepts().size() == 0) {
			String hql = " from Twdxx t where t.tdept.cid = :deptid";
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("deptid", deptid);
			wds = wdxxDao.find(hql, params);
		}
		return listRows(wds);
	}

	@Override
	public void add(Wdxx wdxx) {
		Twdxx twdxx = new Twdxx();
		Date d = new Date();
		BeanUtils.copyProperties(wdxx, twdxx);
		twdxx.setCid(UUID.randomUUID().toString());
		twdxx.setCcreatedate(new Timestamp(d.getTime()));
		twdxx.setCmodifydate(new Timestamp(d.getTime()));
		twdxx.setTdept(deptDao.get(Tdept.class, wdxx.getDeptId()));
		twdxx.setCstate(1);
		wdxxDao.save(setTotalFcxx(twdxx));
	}

	private Twdxx setTotalFcxx(Twdxx t) {
		Double cziyymj = t.getCziyymj();// 自用营业面积
		Double cziqtmj = t.getCziqtmj();// 自用其他面积
		Double cczdmmj = t.getCczdmmj();// 出租店面面积
		Double cczqtmj = t.getCczqtmj();// 出租其他面积
		Double czudmmj = t.getCzudmmj();// 租用店面面积
		Double czuqtmj = t.getCzuqtmj();// 租用其他面积
		Double czimjxj = cziyymj + cziqtmj;// 自用面积小计
		Double cczzmj = cczdmmj + cczqtmj;// 出租面积合计
		Double czuzmj = czudmmj + czuqtmj;// 租用面积合计
		Double czizmj = czimjxj + cczzmj;// 自有房产总面积=自用+出租
		t.setCzimjxj(DoubleUtil.Keep2(czimjxj));
		t.setCczzmj(cczzmj);
		t.setCzuzmj(czuzmj);
		t.setCzizmj(czizmj);
		return t;
	}

	@Override
	public void edit(Wdxx wdxx) {
		Twdxx t = wdxxDao.get(Twdxx.class, wdxx.getCid());
		BeanUtils.copyProperties(wdxx, t, new String[] { "ccreatedate", "cstate", "cid" });
		Date d = new Date();
		t.setCmodifydate(new Timestamp(d.getTime()));
		t.setTdept(deptDao.get(Tdept.class, wdxx.getDeptId()));
		setTotalFcxx(t);
	}

	@Override
	public void delete(Wdxx wdxx) {
		String cids = "";
		if (wdxx.getIds() != null) {
			String ids = wdxx.getIds();
			for (String cid : ids.split(",")) {
				cids += "'" + cid + "'" + ",";
			}
			cids = cids.substring(0, cids.length() - 1);
			String hql = "delete Twdxx t where t.cid in (" + cids + ")";
			wdxxDao.executeHql(hql);
		}

	}

	@Override
	public Wdxx getWdxxById(String id) {
		Twdxx t = wdxxDao.get(Twdxx.class, id);
		Wdxx w = new Wdxx();
		if (t != null) {
			BeanUtils.copyProperties(t, w);
			if (t.getTdept() != null) {
				w.setDeptId(t.getTdept().getCid());
				w.setDeptName(t.getTdept().getCname());
			}
		}
		return w;
	}

	@Override
	public int addWdxxs(Wdxx wdxx) {
		int i = 0;
		String[] fields = { "cds", "cqx", "cname", "caddress", "cphone", "ctype", "czrs", "chtg", "clwg", "ctx", "catm", "ccrs", "cjd", "cwd" };
		if (wdxx != null && !wdxx.getFilename().isEmpty()) {
			ExcelUtil eu = new ExcelUtil();
			List<ArrayList<String>> wdxxList = eu.read(wdxx.getFilename());
			Tdept tdept = deptDao.get(Tdept.class, wdxx.getDeptId());
			if (tdept != null) {
				for (ArrayList<String> row : wdxxList) {
					String wdname = row.get(2).trim();
					if (!StringUtils.isEmpty(wdname)) {
						Twdxx t = new Twdxx();
						t.setCid(UUID.randomUUID().toString());
						t.setTdept(tdept);
						Date d = new Date();
						t.setCcreatedate(new Timestamp(d.getTime()));
						t.setCmodifydate(new Timestamp(d.getTime()));
						t.setCstate(1);
						for (int n = 0; n < row.size(); n++) {
							try {
								org.apache.commons.beanutils.BeanUtils.setProperty(t, fields[n], row.get(n));
							} catch (IllegalAccessException e) {
								e.printStackTrace();
							} catch (InvocationTargetException e) {
								e.printStackTrace();
							}
						}
						wdxxDao.save(setTotalFcxx(t));
						i++;
					}
				}
			}
			File f = new File(wdxx.getFilename());
			if (f.exists() && f.isFile()) {
				f.delete();
			}
		}
		return i;
	}

	@Override
	public List<Wdxx> findWdxxs(Wdxx wdxx) {
		List<Twdxx> wds = new ArrayList<Twdxx>();
		if (wdxx != null && !StringUtils.isEmpty(wdxx.getDeptId()) && wdxx.getDeptIds().size() > 0) {
			String hql = " from Twdxx t where t.tdept.cid in(:deptids)";
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("deptids", wdxx.getDeptIds());
			wds = wdxxDao.find(hql, params);
		}
		return listRows(wds);
	}

}
